/**
 * Created by WLG on 2018/11/12.
 */
const mongoose = require('mongoose');
const redisDB = require('../lib/redisManage');
let serverConfig = require('../config/serverConfig');
let AccountMem = mongoose.model('AccountMem');
/**
 * 账户成员服务
 */
class AccountMemService{
    static async load(item){
        let result = await AccountMem.find(item).exec();
        if(result){
            result = result[0];
        }
        return result;
    }
    static async save(item){
        let tempItem = new AccountMem(item);
        return await tempItem.save();
    }
    static async getCache(id,refresh){
        let result = "{}";
        let key = this.createKey(id);
        let item = null;
        if(refresh){

        }else{
            item = await redisDB.get(key);
        }
        if(item){
            result = item;
        }else {
            result = await this.load({_id:id});
            if (result) {
                await this.setCache(key,result)
            }
        }
        return result;
    }
    static createKey(id){
        return "AccountMem:"+id;
    }
    static async setCache(key,item){
        await redisDB.set(key,item,serverConfig.socketCommonCacheTime)
    }

    static  async searchMemByName(item){
        return await AccountMem.find({name:{$regex:item,$options:'$i'}});
    }

}
module.exports = AccountMemService;